package com.toe.commodity.webapi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Value("${file.staticGoodsImgPatternPath}")
    private String staticGoodsImgPatternPath;

    @Value("${file.staticVideoPatternPath}")
    private String staticVideoPatternPath;

    @Value("${file.staticVideoImgPatternPath}")
    private String staticVideoImgPatternPath;

    @Value("${file.uploadGoodsImgFolder}")
    private String uploadGoodsImgFolder;

    @Value("${file.uploadVideoFolder}")
    private String uploadVideoFolder;

    @Value("${file.uploadVideoImgFolder}")
    private String uploadVideoImgFolder;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler(staticGoodsImgPatternPath)
                .addResourceLocations("file:"+uploadGoodsImgFolder);
        registry.addResourceHandler(staticVideoPatternPath)
                .addResourceLocations("file:"+ uploadVideoFolder);
        registry.addResourceHandler(staticVideoImgPatternPath)
                .addResourceLocations("file:"+ uploadVideoImgFolder);
    }

}
